package kuang;

/**
 * @author cyt
 * @create 2020-03-06 22:15
 */

/**
 * 出现了线程不安全的现象， 多个线程访问同一个资源
 */
public class TicketThread implements Runnable {

    private Integer total = 5;

    public static void main(String[] args) {
        new Thread(new TicketThread(), "A").start();
        new Thread(new TicketThread(), "B").start();
        new Thread(new TicketThread(), "C").start();
    }

    @Override
    public void run() {
        while (true) {
            if (total <= 0) {
                break;
            }
            synchronized (total) {
                System.out.println(Thread.currentThread().getName() + "--->>>" + total--);
            }
        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
